﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using ZeroBug.EasyFlow.Core;

namespace EasyFlow
{
    public partial class VariableViewModel : ObservableObject
    {
        private Variable _variable;
        [ObservableProperty]
        string _name;
        string _value;
        public string Value
        {
            get
            {
                _value = BindingVariable.ToString();
                return _value;
            }
            set
            {
                SetProperty<string>(ref _value, value);
            }
        }

        [ObservableProperty]
        ObservableCollection<VariableViewModel> _children;
        public Variable BindingVariable { get; set; }
        public bool IsNodeOnly { get; set; }
        public bool IsLocal { get; set; }
        public VariableViewModel(Variable variable, bool isLocal = false)
        {
            BindingVariable = variable;
            Name = isLocal ? variable.Name.Split('.')[1] : variable.Name;
            _variable = variable;
            IsLocal = isLocal;
            variable.NotifyVariableChanged += onVariableChanged;
        }

        private void onVariableChanged()
        {
            Value = BindingVariable.ToString();
        }

        public void SetChildren(List<Variable> variables, bool isLocal = false)
        {
            this.Children = new ObservableCollection<VariableViewModel>(variables.Where(v => v.Group.Equals(this.Name)).Select(v => new VariableViewModel(v, isLocal)));
        }
        public Variable GetVariable()
        {
            return _variable;
        }
    }
}
